package com.example.javabasic.mianshi;

import com.example.javabasic.mianshi.fuzhulei.Tree;
import com.example.javabasic.mianshi.fuzhulei.TreeNode;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 二叉树层序遍历   Z字形遍历
 */
public class Cengxu {

    public static void main(String[] args) {
        Tree tree = newTree();
        test(tree);
        test2(tree);
    }
    //创建示例二叉树
    public static Tree newTree(){
        int[] arr = {8,6,10,5,7,9,11};
        Tree tree = new Tree(8);
        for (int i = 1; i < arr.length; i++) {
            tree = setTree(arr[i], tree);
        }
        print(tree);
        return tree;
    }
    //插入二叉树, 二叉查找树
    public static Tree setTree(Integer val,Tree tree){
        Tree now = tree;
        while(now!=null){
            if(now.val>val){
                if(now.left ==null){
                    now.left = new Tree(val);
                    break;
                }
                now = now.left;
            }else{
                if(now.right ==null){
                    now.right = new Tree(val);
                    break;
                }
                now = now.right;
            }
        }
        return tree;
    }
    //输出二叉树 ,前序遍历
    public static void print(Tree tree){
        if(tree ==null){
            return;
        }
        System.out.print(tree.val+" ");
        print(tree.left);
        print(tree.right);
    }


    //二叉树层序遍历


    public static void test(Tree tree){
        List<Integer> data = new ArrayList<>();
        Queue<Tree> queue = new LinkedList();
        queue.offer(tree);
        data.add(tree.val);
        int n = 0;
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while(size>0){
                Tree poll = queue.poll();
                if(poll.left!=null){
                    list.add(poll.left.val);
                    queue.offer(poll.left);
                }
                if(poll.right!=null){
                    list.add(poll.right.val);
                    queue.offer(poll.right);
                }
                size--;
            }
            //下方注释内容为 之字形遍历过程
            if(n%2 == 0){
                data.addAll(list);
            }else{
                Integer[] integers = list.toArray(new Integer[list.size()]);
                for (int i = integers.length - 1; i >= 0; i--) {
                    data.add(integers[i]);
                }
            }
            n++;

        }
        System.out.println(data);

    }



    public static void test2(Tree tree){
        List<Integer> res = new ArrayList<>();
        List<Tree> a = new ArrayList<>();
        List<Tree> b = new ArrayList<>();
        a.add(tree);
        int n = 0;
        while(a!=null){
            n++;
            if(n%2 == 0){
                for (Tree tree1 : a) {
                    res.add(tree1.val);
                }
            }else{
                for (int i = a.size() - 1; i >= 0; i--) {
                    res.add(a.get(i).val);
                }
            }
            for (Tree t : a) {
                if(t.left!=null){
                    b.add(t.left);
                }
                if(t.right!=null){
                    b.add(t.right);
                }
            }
            if(CollectionUtils.isEmpty(b)){
                break;
            }
            a.clear();
            a.addAll(b);
            b.clear();
        }
        System.out.println(res);
    }





}
